package com.woniuxy.rms.service.impl;

import com.woniuxy.rms.dao.PlotsMapper;
import com.woniuxy.rms.dao.PropertyMapper;
import com.woniuxy.rms.dao.UsersMapper;
import com.woniuxy.rms.pojo.Plots;
import com.woniuxy.rms.pojo.Property;
import com.woniuxy.rms.pojo.Users;
import com.woniuxy.rms.service.PlotService;
import com.woniuxy.rms.util.DataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Auther: ChenGang  <br/>
 * Date: 2020/3/29 0029:下午 8:51  <br/>
 * Description:小区服务层
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PlotServiceImpl implements PlotService {

    @Autowired
    private PlotsMapper plotsMapper;

    @Autowired
    private PropertyMapper propertyMapper;

    @Autowired
    private UsersMapper usersMapper;

    @Override
    public List<Plots> findByPage(Integer currentPage, Integer pageSize, Long proId, String plotName, String plotPlace, String company) {
        return plotsMapper.findByPage(currentPage,pageSize,proId,plotName,plotPlace,company);
    }

    @Override
    public DataResult save(Plots plots) {
        plots.setCreateTime(new Date());
        plots.setPlotNum(UUID.randomUUID().toString().trim().replace("-",""));
        plotsMapper.insertSelective(plots);
        return DataResult.success();
    }

    @Override
    public DataResult update(Plots plots) {
        plots.setUpdateTime(new Date());
        plotsMapper.updateByPrimaryKeySelective(plots);
        return DataResult.success();
    }

    @Override
    public DataResult delete(Integer id) {
        try {
            plotsMapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
            DataResult.failed("该小区不能删除！");
        }
        return DataResult.success();
    }

    @Override
    public Plots findById(Integer id) {
        return plotsMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Property> findCompany() {
        return propertyMapper.findCompany();
    }

    @Override
    public List<Users> findUserByPage(Integer currentPage, Integer pageSize, Integer plotId) {
        return usersMapper.findByPlotId(currentPage,pageSize,plotId);
    }

    @Override
    public DataResult userDelete(Long id) {
        usersMapper.deleteByPrimaryKey(id);
        return DataResult.success();
    }

    @Override
    public DataResult checkUser(Users users) {
        usersMapper.updateByPrimaryKeySelective(users);
        return DataResult.success();
    }
}